﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Threading;

namespace WebServices
{
    /// <summary>
    /// Summary description for FirstService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class FirstService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return string.Format("Hello World {0}", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"));
        }

        [WebMethod]
        public string UnstableHelloWorld()
        {
            if (DateTime.Now.Minute % 2 == 0)
            {
                return HelloWorld();
            }
            else
            {
                throw new InvalidOperationException("Failure");
            }
        }

        [WebMethod]
        public string SlowHelloWorld()
        {
            Thread.Sleep(DateTime.Now.Millisecond);
            return HelloWorld();
        }

        [WebMethod]
        public string SlowerHelloWorld()
        {
            Thread.Sleep(3 * DateTime.Now.Millisecond);
            return HelloWorld();
        }

        [WebMethod]
        public string FailingHelloWorld()
        {
            Random rnd = new Random(DateTime.Now.Millisecond);
            int n = rnd.Next(6);
            if (n > 3)
            {
                throw new InvalidOperationException("Internal failure!");
            }
            else
            {
                return HelloWorld();
            }
        }
    }
}
